Page({
  data: {
		isQiang:false,

		prizeData:[],

		shopArr:[
			{
				w:180,
				h:216,
				t:130,
				l:20,
				id:574576989687
			},
			{
				w:184,
				h:211,
				t:134,
				l:238,
				id:527036337530
			},
			{
				w:174,
				h:245,
				t:120,
				l:450,
				id:527050980299
			},
			{
				w:164,
				h:218,
				t:390,
				l:42,
				id:527036505839
			},
			{
				w:160,
				h:234,
				t:366,
				l:254,
				id:527048328080
			},
			{
				w:186,
				h:238,
				t:367,
				l:450,
				id:527049104768
			}
			

		]
	},
	
	seeShop(e){
		
		my.tb.openDetail ({
      itemId: e.target.dataset.id.toString(),
      success: (res) => {
        // my.alert({ content: "success" });
      },
      fail: (res) => {
        // my.alert({ content: "fail - " + res.error });
      },
		});

	},

	//点击了礼物
	clickGift(e){
		console.info(e.target.dataset.type)

		if(e.target.dataset.type){
			console.info('虚拟奖')

			let {prizeData} = this.data

			console.info(prizeData[e.target.dataset.index].gift.url)

			my.call('navigateToOutside',{          
				url:prizeData[e.target.dataset.index].gift.url
			},(res)=>{
				
			})

		}else{
			console.info('实物奖')
			my.authorize({
					scopes: 'scope.addressList',
					success: (resu) => {
						my.tb.chooseAddress({
							addAddress: "show",
							searchAddress: "show",
							locateAddress: "show",
							success:(res) => {
								console.info('地址========>',res)
									
								let address = {
									name:res.name,
									phone:res.telNumber,
									province:res.provinceName,
									city:res.cityName,
									area:res.countyName,
									address:res.detailInfo,
									nick_name:this.data.mineInfo.nickName
								}

								getApp().cloud.function.invoke('draw',address,"saveAddress").then(result => {
									console.log('保存地址===>',result); 

									if(result.code != 0){

										my.showToast({
											content:result.msg
										});

										return
									}

									my.showToast({
										duration:3000,
										content: '地址保存成功'
									})

								})



							}
						})
					}
			})
		}

	},

	//请求我的礼品列表
	obtainMinePrize(){
		getApp().cloud.function.invoke('draw',{},"myPrizeLogList").then(res => {
			console.log('我的礼品列表===>',res); 

			if(res.code != 0){

				my.showToast({
					content:res.msg
				});

				return
			}

			this.setData({
				prizeData:res.data
			})
		})  
	},

	//回到首页
	backMain(){
		my.reLaunch({
			url: '../index/index'
		});
	},

	//开始游戏前判断剩余游戏次数是否充足
	startGame(){

		my.showLoading();

		getApp().cloud.function.invoke('client',{},"getCurrentUserInfo").then(res => {
			console.log('获取剩余游戏次数=====>',res); 

			my.hideLoading();

			if(res.data.game_left_num <= 0){
				my.navigateTo({
					url: '../drawResult/drawResult?end=gameNo'
				});
			}else{
				my.navigateTo({
					url: '../game/game'
				});
			}
		})
		
		
	},

	onShow(){
		this.server()
		this.obtainMinePrize()
	},

	onLoad() {
		// this.server()
		// this.obtainMinePrize()

		my.authorize({
			scopes: 'scope.userInfo',
			success: (res) => {
				my.getAuthUserInfo({
					success: (userInfo) => {
						this.setData({
							mineInfo:userInfo
						})
					}
				});
			},
		});
	},
	
	async server(){
		let timer = await my.getServerTime()
		
		var time = new Date(timer.time);
		var y = time.getFullYear();
		var m = time.getMonth()+1;
		var d = time.getDate();
		

		//判断如果是 2021年3月4号 之后就是抢购
		if(y == 2021 && m == 3 && d>= 5){
			this.setData({
				isQiang:true
			})
		}else{
			this.setData({
				isQiang:false
			})
		}
		
	},
});
